package com.tai.juc;

import org.junit.Test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * The type Safe time util.
 *
 * @description: 线程安全的时间工具类
 * @author: Taylor
 * @date :  2021-04-23 08:22
 */
public class SafeTimeUtil {

    /**
     * Test.
     *
     * @throws ParseException the parse exception
     */
    @Test
    public void test() throws ParseException {

        String str ="2021-04-23 12:23:12.125001";
        String myformat ="yyyy-MM-dd HH:mm:ss.SSSSSS";
        //非线程安全
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(myformat);
        Date date = simpleDateFormat.parse(str);
        long time = date.getTime();

        //线程安全
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(myformat);
        LocalDateTime localDateTime = parseStringToDateTime(str, myformat);
        System.out.println(localDateTime);
        long timestamp = getTimestampOfDateTime(localDateTime);

        System.out.println("localDateTime   :"+timestamp);
        System.out.println("SimpleDateFormat:"+time);

        LocalDateTime dateTimeOfTimestamp = getDateTimeOfTimestamp(timestamp);
        System.out.println(dateTimeOfTimestamp);

        String format = dateTimeOfTimestamp.format(dateTimeFormatter);
        System.out.println("localDateTime   :"+format);

        String format1 = simpleDateFormat.format(new Date(time));
        System.out.println("SimpleDateFormat:"+format1);

    }


    /**
     * 1.将LocalDateTime转为自定义的时间格式的字符串
     *
     * @param localDateTime the local date time
     * @param format        the format
     * @return date time as string
     */
    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    /**
     * 2.将long类型的timestamp转为LocalDateTime
     *
     * @param timestamp the timestamp
     * @return the date time of timestamp
     * @retur
     */
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }


    /**
     * 3.将LocalDateTime转为long类型的timestamp
     *
     * @param localDateTime the local date time
     * @return timestamp of date time
     */
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }


    /**
     * 4.将某时间字符串转为自定义时间格式的LocalDateTime
     *
     * @param time   the time
     * @param format the format
     * @return local date time
     */
    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }
}
